<?php

namespace app\common\model;

/**
 * Description of Dict
 *
 * @author wyx
 */
class Dict {

    ////--------------------------- 1.系统分类 ---------------------------------
    //1.1 状态的一般分类
    const STATUS_PRIMARY = 0; //未启用
    const STATUS_NORMAL = 1; //正常
    const STATUS_DISABLED = 2; //禁用
    const STATUS_DELETED = -1; //删除
    //1.2 是否分类
    const IS_YES = 1; //是
    const IS_NO = 0; //否
    ////--------------------------- 2.业务类型 ---------------------------------
    //2.1 营业类型
    const VILLAGE_TYPE_SELF = 1; //直营
    const VILLAGE_TYPE_TOGETHER = 2; //联营
    ////--------------------------- 3.业务状态 --------------------------------
    //3.1 商品状态
    const GOODS_STATUS_DELETED = -1; //删除
    const GOODS_STATUS_PRIMARY = 0; //未启用
    const GOODS_STATUS_NORMAL = 1; //在售
    const GOODS_STATUS_DISABLED = 2; //已下架

    /*     * ****** 以下为字典分类详情描述  ********** */

    //STATUS 状态分类详情
    protected static $STATUS_CONFIG = array(
        self::STATUS_PRIMARY  => array(
            'id'     => self::STATUS_PRIMARY,
            'name'   => 'primary',
            'filter' => ''
        ),
        self::STATUS_NORMAL   => array(
            'id'     => self::STATUS_NORMAL,
            'name'   => 'normal',
            'filter' => 'shop'
        ),
        self::STATUS_DISABLED => array(
            'id'     => self::STATUS_DISABLED,
            'name'   => 'disabled',
            'filter' => 'shop'
        ),
        self::STATUS_DELETED  => array(
            'id'     => self::STATUS_DELETED,
            'name'   => 'deleted',
            'filter' => ''
        )
    );
    //IS 是否分类详情
    protected static $IS_CONFIG = array(
        self::IS_YES => array(
            'id'   => self::IS_YES,
            'name' => 'yes'
        ),
        self::IS_NO  => array(
            'id'   => self::IS_NO,
            'name' => 'no'
        )
    );
    //GOODS_STATUS 商品状态详情
    protected static $GOODS_STATUS_CONFIG = array(
        self::GOODS_STATUS_PRIMARY  => array(
            'id'   => self::GOODS_STATUS_PRIMARY,
            'name' => 'goods primary'
        ),
        self::GOODS_STATUS_NORMAL   => array(
            'id'   => self::GOODS_STATUS_NORMAL,
            'name' => 'goods normal'
        ),
        self::GOODS_STATUS_DISABLED => array(
            'id'   => self::GOODS_STATUS_DISABLED,
            'name' => 'goods disabled'
        ),
        self::GOODS_STATUS_DELETED  => array(
            'id'   => self::GOODS_STATUS_DELETED,
            'name' => 'goods deleted'
        )
    );

    /**
     * [获取字典配置]
     * @param string $name     字典配置名称
     * @param string $filter   过滤条件部 其它-按过滤条件取
     * @return [array]
     */
    public static function getMagicConfig($name, $filter = 'all') {
        $config = strtoupper($name) . '_CONFIG';
        $returnConfig = self::$$config;
        if ($filter !== 'all') {
            foreach ($returnConfig as $key => &$_one) {
                if (isset($_one['filter']) && $_one['filter'] !== $filter) {
                    unset($returnConfig[$key]);
                }
            }
        }
        return $returnConfig;
    }

    /**
     * [获取字典配置] 用于构建Select/Radio/Checkbox选择列表
     * @param string $name     字典配置名称
     * @param string $filter   过滤条件：all-取全部 其它-按过滤条件取
     * @return [array]
     */
    public static function getMagicData($name, $filter = 'all') {
        $config = strtoupper($name) . '_CONFIG';
        $returnConfig = self::$$config;
        $returnData = array();
        foreach ($returnConfig as $key => &$_one) {
            if (isset($_one['filter']) && $_one['filter'] !== $filter) {
                continue;
            }
            $returnData[$key] = lang($_one['name']);
        }
        return $returnData;
    }

    /**
     * [获取字典标题]
     * @param string $name  字典配置名称
     * @param string $key   字典KEY值
     * @return [array]
     */
    public static function getMagicTitle($name, $key, $field = 'name') {
        $config = strtoupper($name) . '_CONFIG';
        $returnConfig = self::$$config;
        return isset($returnConfig[$key]) ? lang($returnConfig[$key][$field]) : '';
    }

}
